package final2107.day09;

public class ALinkedList<E> {
    private Node head;

    public boolean add(E e){
        Node newNode = new Node(e);
        if (head==null){
            head = newNode;
            return true;
        }
        return head.append(newNode);
    }

    @Override
    public String toString() {
        if (head==null)
            return "[]";
        StringBuilder builder = new StringBuilder("[");
        return head.eachEle(builder);
    }

    public ALinkedList<E> reverse(){
        Node newHead = head;
        if (head==null)
            return this;
        return null;
    }



    private class Node{
        E data;
        Node next;

        Node(E ele){
            this.data = ele;
        }

        public boolean append(Node e) {
            if (next==null){
                next = e;
                return true;
            }
            return next.append(e);
        }

        public String eachEle(StringBuilder builder) {
            builder.append(data).append(",");
            if(next!=null)
                next.eachEle(builder);
            return builder.deleteCharAt(builder.length()-1).append("]").toString();
        }
    }
}
